<?php
/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-09-20
 * 版    本：1.0.0
 * 功能说明：后台首页控制器。
 *
 **/

namespace Qwadmin\Controller;

class LogisticsSupplyController extends ComController
{
    public function index()
    {
		$p = isset($_GET['p']) ? intval($_GET['p']) : '1';
       
        $keyword = isset($_GET['keyword']) ? htmlentities($_GET['keyword']) : '';
        $where = '';

      


        $LogisticsSupply = M('LogisticsSupply');
        $pagesize = 10;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $count = $LogisticsSupply->where($where)->count();

        $list = $LogisticsSupply
            ->where($where)
            ->limit($offset . ',' . $pagesize)
            ->select();
       
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
    
        $this->display();
    }
	
	 //新增物流商
    public function add()
    {
        $this->display('form');
    }

    //新增或修改物流商
    public function edit($id = null)
    {

        $id = intval($id);
        $info = M('LogisticsSupply')->where('id=' . $id)->find();
        $this->assign('info', $info);
        $this->display('form');
    }

    //删除物流商
    public function del()
    {

        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        if ($ids) {
            if (is_array($ids)) {
                $ids = implode(',', $ids);
                $map['id'] = array('in', $ids);
            } else {
                $map = 'id=' . $ids;
            }
            if (M('LogisticsSupply')->where($map)->delete()) {
                addlog('删除物流商，ID：' . $ids);
                $this->success('恭喜，删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
    }
	
	//商家详情
	public function detail()
	{
		$id = intval(I('get.id'));
        $info = M('LogisticsSupply')->where('id=' . $id)->find();
	
        $this->assign('info', $info);
        $this->display();
	}

    //保存物流商
    public function update($id = 0)
    {
        $id = intval($id);
        $data = I('post.');
		$data['province_name'] = M('area')->where(array('id'=>$data['province_id']))->getField('name');
		$data['city_name'] = M('area')->where(array('id'=>$data['city_id']))->getField('name');
		$data['area_name'] = M('area')->where(array('id'=>$data['area_id']))->getField('name');
		
		if($data['passwd']){
			$data['passwd'] = md5($data['passwd']);
			
		}else{
			unset($data['passwd']);
		}
	
        if ($id) {
            M('LogisticsSupply')->data($data)->where('id=' . $id)->save();
            addlog('修改物流商，ID：' . $id);
        } else {
            M('LogisticsSupply')->data($data)->add();
            addlog('新增物流商');
        }

        $this->success('恭喜，操作成功！', U('index'));
    }
	
}